package com.hxd.otc.module.my.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.my.contract.WebViewContract;
import com.hxd.otc.module.my.presenter.WebViewPresenter;
import com.hxd.otc.utils.FileUtils;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.TitleBar;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity<WebViewPresenter> implements WebViewContract.View {
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.webview)
    WebView mWebview;

    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_URL = "url";
    public static final String EXTRA_IS_HELP_CENTER = "help_center";
    public static final String EXTRA_IS_PDF = "isPdf";
    private boolean mIsHelpCenter;
    private boolean mIsPdf = false;//
    private String mTitle;
    private String mUrl;//http://192.168.0.147:8080/demo/demo.pdf

    public static final String EXTEA_TAG = "tag";
    public static final String TAG_REGISTER = "tag_register";
    public static final String TAG_FORGET_PASSWORD = "tag_forget";


    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        Bundle bundle = getBundle();
        if (bundle != null) {
            mIsHelpCenter = bundle.getBoolean(EXTRA_IS_HELP_CENTER);
            mTitle = bundle.getString(EXTRA_TITLE);
            mUrl = bundle.getString(EXTRA_URL);
            mIsPdf = bundle.getBoolean(EXTRA_IS_PDF);
        }
        mTitleBar.setTitleText(mTitle);
        initWebView();


    }


    private void initWebView() {
        setHelpCenterSkipMethod();
        WebSettings settings = mWebview.getSettings();
        settings.setJavaScriptEnabled(true);//  // 设置与Js交互的权限
        settings.setAllowFileAccess(true);//  // 设置与Js交互的权限
        settings.setAllowFileAccessFromFileURLs(true);//  // 设置与Js交互的权限
        settings.setAllowUniversalAccessFromFileURLs(true);//  // 设置与Js交互的权限
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);// 设置允许JS弹窗
      /*  *//**
         * js调用Android本地Java方法
         *
         * obj:暴露的要调用的对象
         * interfaceName：对象的映射名称 ,object的对象名，在js中可以直接调用
         * 在html的js中：JSCall.jumpToLogin()
         * JSCall，这是因为JSCall挂载到js的window对象下了
         *//*
        mWebview.addJavascriptInterface(new DataJavascriptInterface(), "JSCall");*/

        mWebview.loadUrl(mIsPdf ? "file:///android_asset/index.html?" + mUrl : mUrl);

    }

    private void setHelpCenterSkipMethod() {
        if (!mIsHelpCenter) return;
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url;
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    url = request.getUrl().toString();
                } else {
                    url = request.toString();
                }

                Bundle bundle = new Bundle();
                bundle.putString(WebViewActivity.EXTRA_TITLE, getString(R.string.my_help_to_use));
                bundle.putString(WebViewActivity.EXTRA_URL, url);
                $startActivity(WebViewActivity.class, bundle);
                return true;
            }
        });
    }

//
//    // js通信接口
//    public class DataJavascriptInterface {
//
//        public DataJavascriptInterface() {
//        }
//
//        @JavascriptInterface
//        public void jumpToLogin(final String userName, final String psw) {
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    if (TAG_REGISTER.equals(mTag)) {
//                        mPresenter.login(userName, psw, true);
//                    } else if (TAG_FORGET_PASSWORD.equals(mTag)) {
//                        showToast(getString(R.string.my_modify_login_password_success));
//                        finish();
//                    }
//                }
//            });
//
//        }
//    }

    @Override
    protected void initData() {


    }


    @Override
    protected void initEvent() {

        if (mIsPdf) {
            mTitleBar.getRightText().setText(getString(R.string.download));
            mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
                @Override
                public void onBack(View v) {

                }

                @Override
                public void onRight(View v) {
                    mTitleBar.getRightText().setEnabled(false);
                    mPresenter.downloadFile(mUrl, mTitle);

                }
            });
        }

    }

    @Override
    protected WebViewPresenter createPresenter() {
        return new WebViewPresenter(this);
    }


    @Override
    public void downloadFileSuccess() {
        mTitleBar.getRightText().setEnabled(true);
        showToast(getString(R.string.save_download_file_success,FileUtils.outDirectory(UIUtils.getContext(), FileUtils.DOWNLOAD_PDF)));
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        mTitleBar.getRightText().setEnabled(true);
        showToast(getString(R.string.save_file_fail));
    }
}
